package com.lzh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.ThreadLocalUtils;
import com.lzh.bean.*;
import com.lzh.common.bean.PageBean;
import com.lzh.common.ex.BusinessException;
import com.lzh.common.utils.DateUtil;
import com.lzh.mapper.SupplierInquiryMapper;
import com.lzh.mapper.SupplierQuotationMapper;
import com.lzh.query.SupplierQuotationQuery;
import com.lzh.service.SupplierQuotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Max;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class SupplierQuotationServiceImpl implements SupplierQuotationService {

    @Autowired
    private SupplierQuotationMapper supplierQuotationMapper;

    @Autowired
    private SupplierInquiryMapper supplierInquiryMapper;

    String MaxCode;
    /**
     * 查询报价列表
     * @param supplierQuotationQuery
     * @return
     */
    @Override
    public PageBean query(SupplierQuotationQuery supplierQuotationQuery) {
        PageHelper.startPage(supplierQuotationQuery.getPage(),supplierQuotationQuery.getPageSize());
        //获取当前操作人的id
        Map map = ThreadLocalUtils.getInstance().get();
        int id = Integer.parseInt(map.get("id").toString());
        supplierQuotationQuery.setId(id);
        //根据当前操作人id推送
        List query = supplierQuotationMapper.query(supplierQuotationQuery);
        PageInfo pageInfo = new PageInfo(query);
        return new PageBean(supplierQuotationQuery.getPage(),supplierQuotationQuery.getPageSize(),pageInfo.getTotal(),query);
    }


    /**
     * 根据产品id查询供应商
     * @param pid
     * @return
     */
    @Override
    public List<Integer> queryByPid(int pid) {
        List<Integer> integers = supplierQuotationMapper.queryByPid(pid);
        return integers;
    }


    /**
     * 根据id查询相关信息
     * @param id
     * @return
     */
    @Override
    public Map queryByid(int id) {
        Map map = supplierQuotationMapper.queryById(id);
        if (map == null) {
            throw new BusinessException("查询信息失败");
        }
        return map;
    }


    /**
     * 保存供应商报价单（产品询价）
     * @param supplierQuotationCPXJ
     * @return
     */
    public int saveSupplierQuotationCPXJ(SupplierQuotationCPXJ supplierQuotationCPXJ) {
        //自动生成编码
        //获取当前询价人id
        int i = supplierQuotationMapper.saveSupplierQuotationCPXJ(supplierQuotationCPXJ);
        //获取报价单编码,查到询价单id
        String quoteSn = supplierQuotationCPXJ.getQuoteSn();
        if (quoteSn!=null && !"".equals(quoteSn)) {
            //获得询价单id
            int i1 = supplierQuotationMapper.queryBySubCode(quoteSn);
            //根据询价单改变状态
            supplierInquiryMapper.cgStatus(i1,3);
        }
        return i;
    }

    /**
     * 保存供应商报价单（筛网询价）
     * @param supplierQuotationSWXJ
     * @return
     */
    @Override
    public int saveSupplierQuotationSWXJ(SupplierQuotationSWXJ supplierQuotationSWXJ) {
        int i = supplierQuotationMapper.saveSupplierQuotationSWXJ(supplierQuotationSWXJ);
        //获取报价单编码,查到询价单id
        String quoteSn = supplierQuotationSWXJ.getQuoteSn();
        if (quoteSn!=null && !"".equals(quoteSn)) {
            //获得询价单id
            int i1 = supplierQuotationMapper.queryBySubCode(quoteSn);
            //根据询价单改变状态
            supplierInquiryMapper.cgStatus(i1,3);
        }

        return i;
    }

    /**
     * 保存供应商报价单（托辊询价）
     * @param supplierQuotationTGXJ
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveSupplierQuotationTGXJ(SupplierQuotationTGXJ supplierQuotationTGXJ) {
        supplierQuotationTGXJ.getQuoteSn();
        int i = supplierQuotationMapper.saveSupplierQuotationTGXJ(supplierQuotationTGXJ);
        //获取报价单编码,查到询价单id
        String quoteSn = supplierQuotationTGXJ.getQuoteSn();
        if (quoteSn!=null && !"".equals(quoteSn)) {
            //获得询价单id
            int i1 = supplierQuotationMapper.queryBySubCode(quoteSn);
            //根据询价单改变状态
            supplierInquiryMapper.cgStatus(i1,3);
        }

        return i;
    }

    @Override
    public int changeStatus(ChangeStatus changeStatus) {
        int i = supplierQuotationMapper.changeStatus(changeStatus);
        return i;
    }

    /**
     * 保存供应商修改原因
     * @param supplierQuotation
     */
    @Override
    public int saveReason(SupplierQuotation supplierQuotation) {
        supplierQuotation.getSupplierQuotationRequest().setQuotationId(supplierQuotation.getId());
        // 获取当前时间的毫秒数
        long currentTimeMillis = System.currentTimeMillis();
        // 使用毫秒数创建 Timestamp 对象
        Timestamp currentTimestamp = new Timestamp(currentTimeMillis);
        supplierQuotation.getSupplierQuotationRequest().setApplyTime(currentTimestamp);
        int i = supplierQuotationMapper.saveReason(supplierQuotation.getSupplierQuotationRequest());
        return i;
    }


    /**
     *获取编码
     * @return
     */
    //CPXJ20250306DE-03A
//    String getNewCode(){
//        //yyyymmdd
//        String yyyymmdd = DateUtil.getSystemTimeYyyyMmDd();
//        if (MaxCode == null){
//            //如果最大编码是空的就给夫一个初始值
//            MaxCode = supplierQuotationMapper.queryMaxCode();
//            if (MaxCode == null){
//                //说明今天没有开单那么就说第一单
//                MaxCode = "CPXJ"+yyyymmdd+"-"+"1"+"A";
//                return MaxCode;
//            }
//        }
//        //内存中存在最大编码
//        //计算新的编码
//        //判断是否是当月天的
//        if (MaxCode.substring(4,12).equals(yyyymmdd)){
//            //是当天的进行计算
//            int code = Integer.parseInt(MaxCode.substring(14,MaxCode.length() - 1));
//            code = code + 1;
//            MaxCode = MaxCode.substring(0,12)+code+"A";
//            return MaxCode;
//        }
//        //不说当天的重新开单
//        MaxCode = "CPXJ"+yyyymmdd+"-"+"1"+"A";
//
//        return MaxCode;
//    }

}
